package com.xcj.cloud.controller;


import com.xcj.cloud.entity.Page;
import com.xcj.cloud.entity.User;
import com.xcj.cloud.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 用户登录验证
     *
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping("/userLoginVerify")
    public String userLoginVerify(String username, String password) {
        User user = userService.findUserByName(username);
        if (user == null) {
            return "1";
        } else {
            if (!user.getUPassword().equals(password)) {
                return "2";
            } else {
                return "ok";
            }
        }
    }

    /**
     * 跳转到用户主页
     *
     * @param username
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String goToHead(String username, HttpSession session) {
        User user = userService.findUserByName(username);
        if (user != null) {
            //将登录成功的用户存放到session中
            session.setAttribute("user", user);
            return "front/head";
        }
        return "user/login";
    }

    /**
     * 用户名是否存在
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping("/isUsernameExist")
    public String isUsernameExist(String username) {
        User user = userService.findUserByName(username);
        if (user != null) {
            return "此用户名太受欢迎,请更换一个";
        }
        return "ok";
    }

    /**
     * 用户注册验证码验证
     * @param authCode
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/userRegisterVerify")
    public String userRegisterVerify(String authCode, HttpSession session) {
        String randomCode = (String) session.getAttribute("randomCode");
        if (!authCode.equalsIgnoreCase(randomCode)) {
            return "验证码错误!";
        }
        return "ok";
    }

    /**
     * 注册新用户
     * @param username
     * @param uPassword
     * @param session
     * @return
     */
    @RequestMapping("/register_action")
    public String userRegister(String username, String uPassword,
                               HttpSession session) {
        //去除session中的验证码,防止二次注册
        session.removeAttribute("randomCode");
        if (username.equals("") && uPassword.equals("")){
            User user = new User();
            user.setUsername(username).setUPassword(uPassword);
            session.setAttribute("user", user);
            userService.addUser(user);
            return "front/head";
        }
        return null;
    }


    @ResponseBody
    @RequestMapping("page")
    public Page page(int page, int limit) {
        return userService.page((page - 1) * limit, limit);
    }

    @ResponseBody
    @PostMapping("add")
    public String add(User user) {
        userService.add(user);
        return user.toString();
    }

    @ResponseBody
    @RequestMapping("delList")
    public String delList(@RequestBody List<Long> ids) {
        userService.delList(ids);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("del")
    public String delList(long uid) {
        userService.delList(uid);
        return "ok";
    }

    @ResponseBody
    @PostMapping("update")
    public String update(User user) {
        userService.upp(user);
        return null;
    }
}
